package com.mxx.bu_yang.dialog;

import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.mxx.basic.utils.ToastUtils;
import com.mxx.bu_yang.R;
import com.mxx.bu_yang.adapter.TemporaryPositionAdapter;
import com.mxx.bu_yang.response.TemporaryPositionResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：刘江飞
 * 时间：2020/10/14  15:14
 * 邮箱：642371742Z@qq.com
 * 描述：临时库位
 **/
public class TemporaryPositionDialog extends CommonListDialog<TemporaryPositionResponse> implements View.OnClickListener {
    private TextView tv_name;
    private TextView tv_cancel;
    private TextView tv_sure;
    private final Map<String, Object> params = new HashMap<>();
    public TemporaryPositionDialog() {
    }

    /**
     * Sets layout id.
     *
     * @return the layout id
     */
    @Override
    protected int setLayoutId() {
        return R.layout.dialog_temporary_position;
    }

    @Override
    protected void initView() {
        super.initView();
        tv_name = mRootView.findViewById(R.id.tv_name);
        tv_cancel = mRootView.findViewById(R.id.tv_cancel);
        tv_sure = mRootView.findViewById(R.id.tv_sure);
        String list = (String) getSerializableValue("list");
        params.put("list", list);
    }

    @Override
    protected BaseQuickAdapter createAdapter() {
        return new TemporaryPositionAdapter(R.layout.adapter_common_drown_down);
    }

    @Override
    protected void loadServerData() {

    }

    @Override
    protected void setListener() {
        super.setListener();
        tv_name.setOnClickListener(this);
        tv_cancel.setOnClickListener(this);
        tv_sure.setOnClickListener(this);
    }

    @Override
    public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
        if (view.getId() == R.id.tv_name) {
            TemporaryPositionResponse response = (TemporaryPositionResponse) adapter.getItem(position);
            if (response == null) {
                return;
            }
            params.put("response", response);
            tv_name.setText(response.wareName);
            rv_list.setVisibility(View.GONE);
        }
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.tv_name) {
            showResultRecycle();
        }
        if (v.getId() == R.id.tv_cancel) {
            dismiss();
        }
        if (v.getId() == R.id.tv_sure) {
            if (backDataListener == null) {
                return;
            }
            TemporaryPositionResponse response = (TemporaryPositionResponse) params.get("response");
            if (response == null) {
                ToastUtils.showToast(mActivity, "请选择临时库位");
                return;
            }
            dismiss();
            backDataListener.backData(response);
        }

    }

    private void showResultRecycle() {
        List<TemporaryPositionResponse> list = getList();
        if (list == null) {
            return;
        }
        ((TemporaryPositionAdapter) mAdapter).setList(list);
        rv_list.setVisibility(View.VISIBLE);
    }

    private List<TemporaryPositionResponse> getList() {
        String s = (String) params.get("list");
        return new Gson().fromJson(s, new TypeToken<List<TemporaryPositionResponse>>() {
        }.getType());
    }
}
